package cn.cnhis.online.mymodule.Utils;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Created by wuzhc on 2018/10/26.
 */

public class MyPopUtil extends PopupWindow {

    /**
     * 设置pop
     * @param view
     * @return PopupWindow
     */
    public static PopupWindow initPop(View view) {
        return initPop(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    }

    /**
     * 设置pop
     * @param view 布局
     * @param width 宽
     * @param height 高
     * @return popupwindow  默认点击外部可消失
     */
    public static PopupWindow initPop(View view, int width, int height) {
        return initPop(view, width, height, true);
    }

    /**
     * 设置pop
     * @param view
     * @param width
     * @param height
     * @param mOutsideTouchable  点击外部消失
     * @return popupwindow
     */
    public static PopupWindow initPop(View view, int width, int height, boolean mOutsideTouchable) {
        return initPop(view, width, height, mOutsideTouchable, new PaintDrawable());
    }

    /**
     *
     * @param view
     * @param width
     * @param height
     * @param mOutsideTouchable
     * @param mDrawable 背景
     * @return
     */
    public static PopupWindow initPop(View view, int width, int height, boolean mOutsideTouchable, Drawable mDrawable) {
        PopupWindow pop = new PopupWindow(view, width, height);
        pop.setOutsideTouchable(mOutsideTouchable);
        pop.setFocusable(true);
        pop.setTouchable(true);
        pop.setBackgroundDrawable(mDrawable);

        return pop;
    }

    /**
     * 更新pop
     *
     * @param pop
     * @param view
     * @return
     */
    public static PopupWindow updatePop(PopupWindow pop, View view) {
        pop.setContentView(view);
        pop.update();
        return pop;
    }

    /**
     * 隐藏
     * @param pop
     */
    public static void disPop(PopupWindow pop) {
        if (pop != null && pop.isShowing()) {
            pop.dismiss();
        }
    }

    /**
     * 设置 pop 在软件盘之上
     * @param pop
     */
    @SuppressLint("WrongConstant")
    public static void setPopAboveSoftInput(PopupWindow pop) {
        if (pop != null) {
            pop.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
            pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        }
    }
}
